package com.network.topo.snmp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.snmp4j.Target;

import com.network.util.snmp4j.SnmpGet;
import com.network.util.snmp4j.SnmpGetResultSet;

public final class IfTable extends AbstractXxxTable
{
	private static ArrayList<String> oidList = null;
	private HashMap<Object, RowData> objDataMap = new HashMap<Object, RowData>();
	private HashMap<Object, RowData> indexDataMap = new HashMap<Object, RowData>();
	private int rowCount = 0;

	private IfTable(SnmpGetResultSet resultSet)
	{
		this.resultSet = resultSet;
		if (this.resultSet != null)
		{
			rowCount = this.resultSet.getRowCount();
			for (int i = 0; i < rowCount; i++)
			{
				RowData rowData = new RowData();
				int oidIndex = 0;
				rowData.ifIndex = str2int(getValue(getOidList().get(oidIndex++), i));
				rowData.ifDescr = getValue(getOidList().get(oidIndex++), i);
				rowData.ifType = str2int(getValue(getOidList().get(oidIndex++), i));
				// rowData.ifMtu =
				// str2int(getValue(getOidList().get(oidIndex++), i));
				rowData.ifSpeed = str2long(getValue(getOidList().get(oidIndex++), i));
				rowData.ifPhysAddress = getValue(getOidList().get(oidIndex++), i);
				 rowData.ifOperStatus = str2int(getValue(getOidList().get(oidIndex++), i));
				// rowData.ifAdminStatus =
				// str2int(getValue(getOidList().get(oidIndex++), i));
				/*
				 * rowData.ifOperStatus =
				 * str2int(getValue(getOidList().get(oidIndex++), i));
				 * rowData.ifLastChange = getValue(getOidList().get(oidIndex++),
				 * i); rowData.ifInOctets =
				 * str2long(getValue(getOidList().get(oidIndex++), i));
				 * rowData.ifInUcastPkts =
				 * str2long(getValue(getOidList().get(oidIndex++), i));
				 * rowData.ifInNUcastPkts =
				 * str2long(getValue(getOidList().get(oidIndex++), i));
				 * rowData.ifInDiscards =
				 * str2long(getValue(getOidList().get(oidIndex++), i));
				 * rowData.ifInErrors =
				 * str2long(getValue(getOidList().get(oidIndex++), i));
				 * rowData.ifInUnknownProtos =
				 * str2long(getValue(getOidList().get(oidIndex++), i));
				 * rowData.ifOutOctets =
				 * str2long(getValue(getOidList().get(oidIndex++), i));
				 * rowData.ifOutUcastPkts =
				 * str2long(getValue(getOidList().get(oidIndex++), i));
				 * rowData.ifOutNUcastPkts =
				 * str2long(getValue(getOidList().get(oidIndex++), i));
				 * rowData.ifOutDiscards =
				 * str2long(getValue(getOidList().get(oidIndex++), i));
				 * rowData.ifOutErrors =
				 * str2long(getValue(getOidList().get(oidIndex++), i));
				 * rowData.ifOutQLen =
				 * str2long(getValue(getOidList().get(oidIndex++), i));
				 * rowData.ifSpecific = getValue(getOidList().get(oidIndex++),
				 * i);
				 */
				indexDataMap.put(i, rowData);
				objDataMap.put(rowData.getIfIndex(), rowData);
				objDataMap.put(rowData.getIfPhysAddress(), rowData);
			}
		}
	}

	public static IfTable getInstance(SnmpGet snmpGet, Target target)
	{
		SnmpGetResultSet sgrs = null;
		IfTable instance = null;
		try
		{
			sgrs = snmpGet.query(getOidList(), target);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		if (sgrs != null && sgrs.next() == true)
		{
			instance = new IfTable(sgrs);
		}
		return instance;
	}

	public static synchronized ArrayList<String> getOidList()
	{
		if (oidList == null)
		{
			oidList = new ArrayList<String>();
			oidList.add("1.3.6.1.2.1.2.2.1.1");
			oidList.add("1.3.6.1.2.1.2.2.1.2");
			oidList.add("1.3.6.1.2.1.2.2.1.3");
			// oidList.add("1.3.6.1.2.1.2.2.1.4");
			oidList.add("1.3.6.1.2.1.2.2.1.5");
			oidList.add("1.3.6.1.2.1.2.2.1.6");
			oidList.add("1.3.6.1.2.1.2.2.1.8");
			// oidList.add("1.3.6.1.2.1.2.2.1.7");
			/*
			 * oidList.add("1.3.6.1.2.1.2.2.1.8");
			 * oidList.add("1.3.6.1.2.1.2.2.1.9");
			 * oidList.add("1.3.6.1.2.1.2.2.1.10");
			 * oidList.add("1.3.6.1.2.1.2.2.1.11");
			 * oidList.add("1.3.6.1.2.1.2.2.1.12");
			 * oidList.add("1.3.6.1.2.1.2.2.1.13");
			 * oidList.add("1.3.6.1.2.1.2.2.1.14");
			 * oidList.add("1.3.6.1.2.1.2.2.1.15");
			 * oidList.add("1.3.6.1.2.1.2.2.1.16");
			 * oidList.add("1.3.6.1.2.1.2.2.1.17");
			 * oidList.add("1.3.6.1.2.1.2.2.1.18");
			 * oidList.add("1.3.6.1.2.1.2.2.1.19");
			 * oidList.add("1.3.6.1.2.1.2.2.1.20");
			 * oidList.add("1.3.6.1.2.1.2.2.1.21");
			 * oidList.add("1.3.6.1.2.1.2.2.1.22");
			 */
		}
		return oidList;
	}

	public int getIfIndex(Object obj, boolean useIndex)
	{
		int rtnValue = 0;
		RowData rowData = null;
		if (useIndex)
		{
			rowData = indexDataMap.get(obj);
		}
		else
		{
			rowData = objDataMap.get(obj);
		}
		if (rowData != null)
		{
			rtnValue = rowData.getIfIndex();
		}
		return rtnValue;
	}

	public String getIfDescr(Object obj, boolean useIndex)
	{
		String rtnValue = "";
		RowData rowData = null;
		if (useIndex)
		{
			rowData = indexDataMap.get(obj);
		}
		else
		{
			rowData = objDataMap.get(obj);
		}
		if (rowData != null)
		{
			rtnValue = rowData.getIfDescr();
		}
		return rtnValue;
	}

	public int getIfType(Object obj, boolean useIndex)
	{
		int rtnValue = 0;
		RowData rowData = null;
		if (useIndex)
		{
			rowData = indexDataMap.get(obj);
		}
		else
		{
			rowData = objDataMap.get(obj);
		}
		if (rowData != null)
		{
			rtnValue = rowData.getIfType();
		}
		return rtnValue;
	}

	public int getIfMtu(Object obj, boolean useIndex)
	{
		int rtnValue = 0;
		RowData rowData = null;
		if (useIndex)
		{
			rowData = indexDataMap.get(obj);
		}
		else
		{
			rowData = objDataMap.get(obj);
		}
		if (rowData != null)
		{
			rtnValue = rowData.getIfMtu();
		}
		return rtnValue;
	}

	public long getIfSpeed(Object obj, boolean useIndex)
	{
		long rtnValue = 0;
		RowData rowData = null;
		if (useIndex)
		{
			rowData = indexDataMap.get(obj);
		}
		else
		{
			rowData = objDataMap.get(obj);
		}
		if (rowData != null)
		{
			rtnValue = rowData.getIfSpeed();
		}
		return rtnValue;
	}

	public String getIfPhysAddress(Object obj, boolean useIndex)
	{
		String rtnValue = "";
		RowData rowData = null;
		if (useIndex)
		{
			rowData = indexDataMap.get(obj);
		}
		else
		{
			rowData = objDataMap.get(obj);
		}
		if (rowData != null)
		{
			rtnValue = rowData.getIfPhysAddress();
		}
		return rtnValue;
	}

	public int getIfAdminStatus(Object obj, boolean useIndex)
	{
		int rtnValue = 0;
		RowData rowData = null;
		if (useIndex)
		{
			rowData = indexDataMap.get(obj);
		}
		else
		{
			rowData = objDataMap.get(obj);
		}
		if (rowData != null)
		{
			rtnValue = rowData.getIfAdminStatus();
		}
		return rtnValue;
	}

	public int getIfOperStatus(Object obj, boolean useIndex)
	{
		int rtnValue = 0;
		RowData rowData = null;
		if (useIndex)
		{
			rowData = indexDataMap.get(obj);
		}
		else
		{
			rowData = objDataMap.get(obj);
		}
		if (rowData != null)
		{
			rtnValue = rowData.getIfOperStatus();
		}
		return rtnValue;
	}

	public String getIfLastChange(Object obj, boolean useIndex)
	{
		String rtnValue = "";
		RowData rowData = null;
		if (useIndex)
		{
			rowData = indexDataMap.get(obj);
		}
		else
		{
			rowData = objDataMap.get(obj);
		}
		if (rowData != null)
		{
			rtnValue = rowData.getIfLastChange();
		}
		return rtnValue;
	}

	public long getIfInOctets(Object obj, boolean useIndex)
	{
		long rtnValue = 0;
		RowData rowData = null;
		if (useIndex)
		{
			rowData = indexDataMap.get(obj);
		}
		else
		{
			rowData = objDataMap.get(obj);
		}
		if (rowData != null)
		{
			rtnValue = rowData.getIfInOctets();
		}
		return rtnValue;
	}

	public long getIfInUcastPkts(Object obj, boolean useIndex)
	{
		long rtnValue = 0;
		RowData rowData = null;
		if (useIndex)
		{
			rowData = indexDataMap.get(obj);
		}
		else
		{
			rowData = objDataMap.get(obj);
		}
		if (rowData != null)
		{
			rtnValue = rowData.getIfInUcastPkts();
		}
		return rtnValue;
	}

	public long getIfInNUcastPkts(Object obj, boolean useIndex)
	{
		long rtnValue = 0;
		RowData rowData = null;
		if (useIndex)
		{
			rowData = indexDataMap.get(obj);
		}
		else
		{
			rowData = objDataMap.get(obj);
		}
		if (rowData != null)
		{
			rtnValue = rowData.getIfInNUcastPkts();
		}
		return rtnValue;
	}

	public long getIfInDiscards(Object obj, boolean useIndex)
	{
		long rtnValue = 0;
		RowData rowData = null;
		if (useIndex)
		{
			rowData = indexDataMap.get(obj);
		}
		else
		{
			rowData = objDataMap.get(obj);
		}
		if (rowData != null)
		{
			rtnValue = rowData.getIfInDiscards();
		}
		return rtnValue;
	}

	public long getIfInErrors(Object obj, boolean useIndex)
	{
		long rtnValue = 0;
		RowData rowData = null;
		if (useIndex)
		{
			rowData = indexDataMap.get(obj);
		}
		else
		{
			rowData = objDataMap.get(obj);
		}
		if (rowData != null)
		{
			rtnValue = rowData.getIfInErrors();
		}
		return rtnValue;
	}

	public long getIfInUnknownProtos(Object obj, boolean useIndex)
	{
		long rtnValue = 0;
		RowData rowData = null;
		if (useIndex)
		{
			rowData = indexDataMap.get(obj);
		}
		else
		{
			rowData = objDataMap.get(obj);
		}
		if (rowData != null)
		{
			rtnValue = rowData.getIfInUnknownProtos();
		}
		return rtnValue;
	}

	public long getIfOutOctets(Object obj, boolean useIndex)
	{
		long rtnValue = 0;
		RowData rowData = null;
		if (useIndex)
		{
			rowData = indexDataMap.get(obj);
		}
		else
		{
			rowData = objDataMap.get(obj);
		}
		if (rowData != null)
		{
			rtnValue = rowData.getIfOutOctets();
		}
		return rtnValue;
	}

	public long getIfOutUcastPkts(Object obj, boolean useIndex)
	{
		long rtnValue = 0;
		RowData rowData = null;
		if (useIndex)
		{
			rowData = indexDataMap.get(obj);
		}
		else
		{
			rowData = objDataMap.get(obj);
		}
		if (rowData != null)
		{
			rtnValue = rowData.getIfOutUcastPkts();
		}
		return rtnValue;
	}

	public long getIfOutNUcastPkts(Object obj, boolean useIndex)
	{
		long rtnValue = 0;
		RowData rowData = null;
		if (useIndex)
		{
			rowData = indexDataMap.get(obj);
		}
		else
		{
			rowData = objDataMap.get(obj);
		}
		if (rowData != null)
		{
			rtnValue = rowData.getIfOutNUcastPkts();
		}
		return rtnValue;
	}

	public long getIfOutDiscards(Object obj, boolean useIndex)
	{
		long rtnValue = 0;
		RowData rowData = null;
		if (useIndex)
		{
			rowData = indexDataMap.get(obj);
		}
		else
		{
			rowData = objDataMap.get(obj);
		}
		if (rowData != null)
		{
			rtnValue = rowData.getIfOutDiscards();
		}
		return rtnValue;
	}

	public long getIfOutErrors(Object obj, boolean useIndex)
	{
		long rtnValue = 0;
		RowData rowData = null;
		if (useIndex)
		{
			rowData = indexDataMap.get(obj);
		}
		else
		{
			rowData = objDataMap.get(obj);
		}
		if (rowData != null)
		{
			rtnValue = rowData.getIfOutErrors();
		}
		return rtnValue;
	}

	public long getIfOutQLen(Object obj, boolean useIndex)
	{
		long rtnValue = 0;
		RowData rowData = null;
		if (useIndex)
		{
			rowData = indexDataMap.get(obj);
		}
		else
		{
			rowData = objDataMap.get(obj);
		}
		if (rowData != null)
		{
			rtnValue = rowData.getIfOutQLen();
		}
		return rtnValue;
	}

	public String getIfSpecific(Object obj, boolean useIndex)
	{
		String rtnValue = "";
		RowData rowData = null;
		if (useIndex)
		{
			rowData = indexDataMap.get(obj);
		}
		else
		{
			rowData = objDataMap.get(obj);
		}
		if (rowData != null)
		{
			rtnValue = rowData.getIfSpecific();
		}
		return rtnValue;
	}
	public class RowData
	{
		private int ifIndex = 0;
		private String ifDescr = "";
		private int ifType = 0;
		private int ifMtu = 0;
		private long ifSpeed = 0;
		private String ifPhysAddress = "";
		private int ifAdminStatus = 0;
		private int ifOperStatus = 0;
		private String ifLastChange = "";
		private long ifInOctets = 0;
		private long ifInUcastPkts = 0;
		private long ifInNUcastPkts = 0;
		private long ifInDiscards = 0;
		private long ifInErrors = 0;
		private long ifInUnknownProtos = 0;
		private long ifOutOctets = 0;
		private long ifOutUcastPkts = 0;
		private long ifOutNUcastPkts = 0;
		private long ifOutDiscards = 0;
		private long ifOutErrors = 0;
		private long ifOutQLen = 0;
		private String ifSpecific = "";

		public int getIfAdminStatus()
		{
			return ifAdminStatus;
		}

		public void setIfAdminStatus(int ifAdminStatus)
		{
			this.ifAdminStatus = ifAdminStatus;
		}

		public String getIfDescr()
		{
			return ifDescr;
		}

		public void setIfDescr(String ifDescr)
		{
			this.ifDescr = ifDescr;
		}

		public int getIfIndex()
		{
			return ifIndex;
		}

		public void setIfIndex(int ifIndex)
		{
			this.ifIndex = ifIndex;
		}

		public long getIfInDiscards()
		{
			return ifInDiscards;
		}

		public void setIfInDiscards(long ifInDiscards)
		{
			this.ifInDiscards = ifInDiscards;
		}

		public long getIfInErrors()
		{
			return ifInErrors;
		}

		public void setIfInErrors(long ifInErrors)
		{
			this.ifInErrors = ifInErrors;
		}

		public long getIfInNUcastPkts()
		{
			return ifInNUcastPkts;
		}

		public void setIfInNUcastPkts(long ifInNUcastPkts)
		{
			this.ifInNUcastPkts = ifInNUcastPkts;
		}

		public long getIfInOctets()
		{
			return ifInOctets;
		}

		public void setIfInOctets(long ifInOctets)
		{
			this.ifInOctets = ifInOctets;
		}

		public long getIfInUcastPkts()
		{
			return ifInUcastPkts;
		}

		public void setIfInUcastPkts(long ifInUcastPkts)
		{
			this.ifInUcastPkts = ifInUcastPkts;
		}

		public long getIfInUnknownProtos()
		{
			return ifInUnknownProtos;
		}

		public void setIfInUnknownProtos(long ifInUnknownProtos)
		{
			this.ifInUnknownProtos = ifInUnknownProtos;
		}

		public String getIfLastChange()
		{
			return ifLastChange;
		}

		public void setIfLastChange(String ifLastChange)
		{
			this.ifLastChange = ifLastChange;
		}

		public int getIfMtu()
		{
			return ifMtu;
		}

		public void setIfMtu(int ifMtu)
		{
			this.ifMtu = ifMtu;
		}

		public int getIfOperStatus()
		{
			return ifOperStatus;
		}

		public void setIfOperStatus(int ifOperStatus)
		{
			this.ifOperStatus = ifOperStatus;
		}

		public long getIfOutDiscards()
		{
			return ifOutDiscards;
		}

		public void setIfOutDiscards(long ifOutDiscards)
		{
			this.ifOutDiscards = ifOutDiscards;
		}

		public long getIfOutErrors()
		{
			return ifOutErrors;
		}

		public void setIfOutErrors(long ifOutErrors)
		{
			this.ifOutErrors = ifOutErrors;
		}

		public long getIfOutNUcastPkts()
		{
			return ifOutNUcastPkts;
		}

		public void setIfOutNUcastPkts(long ifOutNUcastPkts)
		{
			this.ifOutNUcastPkts = ifOutNUcastPkts;
		}

		public long getIfOutOctets()
		{
			return ifOutOctets;
		}

		public void setIfOutOctets(long ifOutOctets)
		{
			this.ifOutOctets = ifOutOctets;
		}

		public long getIfOutQLen()
		{
			return ifOutQLen;
		}

		public void setIfOutQLen(long ifOutQLen)
		{
			this.ifOutQLen = ifOutQLen;
		}

		public long getIfOutUcastPkts()
		{
			return ifOutUcastPkts;
		}

		public void setIfOutUcastPkts(long ifOutUcastPkts)
		{
			this.ifOutUcastPkts = ifOutUcastPkts;
		}

		public String getIfPhysAddress()
		{
			return ifPhysAddress;
		}

		public void setIfPhysAddress(String ifPhysAddress)
		{
			this.ifPhysAddress = ifPhysAddress;
		}

		public String getIfSpecific()
		{
			return ifSpecific;
		}

		public void setIfSpecific(String ifSpecific)
		{
			this.ifSpecific = ifSpecific;
		}

		public long getIfSpeed()
		{
			return ifSpeed;
		}

		public void setIfSpeed(long ifSpeed)
		{
			this.ifSpeed = ifSpeed;
		}

		public int getIfType()
		{
			return ifType;
		}

		public void setIfType(int ifType)
		{
			this.ifType = ifType;
		}
	}

	public int getRowCount()
	{
		return rowCount;
	}

	public void setRowCount(int rowCount)
	{
		this.rowCount = rowCount;
	}
}
